using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Test {
	class ImgProc {
		static void Main() {
			Bitmap bm=Bitmap.FromFile(@"C:\img\rad.jpg") as Bitmap;
			Bitmap bm1=CropImage(bm,Square(bm)) as Bitmap;
			bm1.Save(@"C:\img\crop.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
			Bitmap bm2=(Bitmap)bm1.GetThumbnailImage(100,100,null,IntPtr.Zero);
			bm2.Save(@"C:\img\thumb1.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
			Bitmap bm3=(Bitmap)Thumbnail(bm1,100,100);
			bm3.Save(@"C:\img\thumb2.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
		}
		private static Image CropImage(Image img,Rectangle cropArea) {
			Bitmap bmpImage=new Bitmap(img);
			Bitmap bmpCrop=bmpImage.Clone(cropArea,bmpImage.PixelFormat);
			return bmpCrop;
		}
		private static Rectangle Square(Image img) {
			int minDim=Math.Min(img.Width,img.Height);
			int left=(img.Width-minDim)/2;
			int top=(img.Height-minDim)/2;
			return new Rectangle(left,top,minDim,minDim);
		}
		private static Image Thumbnail(Image image,int thumbWidth,int thumbHeight) {
			using(Bitmap bmp=new Bitmap(thumbWidth,thumbHeight))
			{
				System.Drawing.Graphics gr=System.Drawing.Graphics.FromImage(bmp);
				gr.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
				gr.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
				gr.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;
				System.Drawing.Rectangle rectDestination=new System.Drawing.Rectangle(0,0,thumbWidth,thumbHeight);
				gr.DrawImage(image,rectDestination,0,0,image.Width,image.Height,GraphicsUnit.Pixel);
				return bmp.Clone() as Bitmap;
			}
		}
	}
}
